import os

import pandas as pd

from foftact.etf import etf_pe

# 将当前目录添加到Python搜索路径中
os.sys.path.append(os.path.dirname(os.path.abspath(__file__)))

funds_pe_data = etf_pe.collect_pe()

for column in funds_pe_data.columns:
    print(f'column : {column}')

# 将 tdate 列转换为 datetime 类型
funds_pe_data['tdate2'] = pd.to_datetime(funds_pe_data['tdate'])
# 按月份分组并获取每个月的最新数据
funds_pe_data['month'] = funds_pe_data['tdate2'].dt.to_period('M')
latest_data = funds_pe_data.sort_values('tdate').groupby('month').last().reset_index(drop=True)
funds_pe_data = latest_data

etf_list = list()

# 将 fund 填充进 etf 实例
for fund in funds_pe_data.values:
    print(f'fund instance: {fund}')
    columns = funds_pe_data.columns
    fund_map = dict(zip(columns, fund))
    print(f'fund_map: {fund_map}')
    etf = etf_pe.transform(fund_map)
    print(f'ETF instance: {etf.to_dict()}')
    etf_list.append(etf)

# etf_pe.batch_save(etf_list, batch_size=1, is_print=True)



